﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LotteryVote.Logic.Web;

namespace LotteryVote.Controls
{
    public partial class Pager : System.Web.UI.UserControl
    {
        private int pager;
        /// <summary>
        /// 样式表
        /// </summary>
        /// <value>The class.</value>
        public string Class { get; set; }

        /// <summary>
        /// 当前页序
        /// </summary>
        /// <value>The pager.</value>
        public int CurrentPager { get { return pager; } }
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        /// <summary>
        /// 绑定分页
        /// </summary>
        /// <param name="totalCount">数据总数量</param>
        /// <param name="itemCount">绑定数量.</param>
        public void BindPager(int totalCount, int itemCount)
        {
            int fromCount = (pager - 1) * itemCount;
            int toCount = fromCount + itemCount;
            toCount = totalCount > toCount ? toCount : totalCount;
            int pageCount = totalCount % itemCount == 0 ? totalCount / itemCount : (totalCount / itemCount) + 1;
            if (pageCount == 0)
                pageCount = 1;
            ltPager.Text = PagerBuilder.Build(pager, pageCount, 9);
        }

        /// <summary>
        ///初始化分页
        /// </summary>
        public void InitPager()
        {
            if (string.IsNullOrEmpty(Request["pageIndex"]))
                pager = 1;
            else
            {
                pager = int.Parse(Request["pageIndex"]);
                if (pager <= 0)
                    pager = 1;
            }
        }
    }
}